package ac.neec.mio.ui.activity;

import java.util.List;

import ac.neec.data.RecordDate;
import ac.neec.data.RecordDetail;
import ac.neec.data.User;
import ac.neec.db.service.SQLManager;
import ac.neec.mio.R;
import ac.neec.mio.ui.graph.HeartRateGraph;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MeasurementResultActivity extends Activity {
	private User user;
	private RecordDetail detail;
	private TextView sportsDetail;
	private TextView theoryHeartRate;
	private TextView theoryCalorie;
	private TextView theoryMets;
	private TextView theoryLevel;
	private TextView pointHeartRate;
	private TextView pointCalorie;
	private TextView pointMets;
	private TextView pointLevel;
	private TextView time;
	private Button saveButton;
	private Button finishButton;
	private HeartRateGraph heartRateGraph;
	private View.OnClickListener mOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v == saveButton) {
				alertCompleateSave(recordSave());
			}
			else if (v == finishButton) {
				finish();
			}
		}

	};

	private boolean recordSave() {
		RecordDate rd = new RecordDate(detail.getRecordDate().getDate());
		int index = SQLManager.insertRecordDate(rd.getDate());
		return (SQLManager.insertRecordDetail(index, detail.getActivities().getActivitiesID(),
				detail.getMeasurementTime(),
				detail.getHeartRateTheory(), detail.getHeartRatePoint(),
				detail.getCalorieTheory(), detail.getCaloriePoint(), detail.getBytes()) > 0) ? true : false;

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_measurement_result);

		user = (User) getIntent().getSerializableExtra("user");
		detail = (RecordDetail) getIntent().getSerializableExtra("detail");

		setlinkView();
		setListener();
		setViewData();
		setHeartRateGraph();
	}

	private void setlinkView() {
		sportsDetail = (TextView) findViewById(R.id.act_measurement_result_type_sports);
		theoryHeartRate = (TextView) findViewById(R.id.act_measurement_result_theory_heartrate);
		theoryCalorie = (TextView) findViewById(R.id.act_measurement_result_theory_calorie);
		theoryMets = (TextView) findViewById(R.id.act_measurement_result_theory_mets);
		theoryLevel = (TextView) findViewById(R.id.act_measurement_result_theory_strength);
		pointHeartRate = (TextView) findViewById(R.id.act_measurement_result_point_heartrate);
		pointCalorie = (TextView) findViewById(R.id.act_measurement_result_point_calorie);
		pointMets = (TextView) findViewById(R.id.act_measurement_result_point_mets);
		pointLevel = (TextView) findViewById(R.id.act_measurement_result_point_strength);
		time = (TextView) findViewById(R.id.act_measurement_result_time);

		saveButton = (Button) findViewById(R.id.act_measurement_result_button_save);
		finishButton = (Button) findViewById(R.id.act_measurement_result_button_stop);

		heartRateGraph = (HeartRateGraph)findViewById(R.id.graph_heart_rate);
	}

	private void setListener() {
		saveButton.setOnClickListener(mOnClickListener);
		finishButton.setOnClickListener(mOnClickListener);
	}

	private void setViewData() {
		sportsDetail.setText(detail.getActivities().getDetailJP());
		theoryHeartRate.setText(String.valueOf(detail.getHeartRateTheory()));
		theoryCalorie.setText(String.valueOf(detail.getCalorieTheory()));
		theoryMets.setText(detail.getActivities().getMetsString());
		theoryLevel.setText(getIntent().getIntExtra("theoryLevel", 0) + "%");
		pointHeartRate.setText(String.valueOf(detail.getHeartRatePoint()));
		pointCalorie.setText(String.valueOf(detail.getCaloriePoint()));
		pointMets.setText(String.valueOf(getIntent().getFloatExtra("pointMETs", -1)));
		pointLevel.setText(getIntent().getIntExtra("pointLevel", 0) + "%");
		time.setText(detail.getMeasurementTime());
	}

	private void setHeartRateGraph() {
		List<Integer> graphData = detail.getGraph();
		heartRateGraph.createChart(graphData);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
		case R.id.action_settings:
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void alertCompleateSave(final boolean flag) {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
		alertDialogBuilder.setTitle("Training Data Saved ....,");
		if (flag)
			alertDialogBuilder.setMessage("Compleate Data Saved\n" +
					"トレーニングデータのセーブが完了しました");
		else
			alertDialogBuilder.setMessage("Uncompleate Data Saved\n" +
					"トレーニングデータのセーブに失敗しました");
		alertDialogBuilder.setPositiveButton("OK",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (flag)
							finish();
					}
				});
		alertDialogBuilder.setCancelable(true);
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();

	}

}
